package com.cskaoyan.service;

import com.cskaoyan.bean.Groupon_rules;
import com.cskaoyan.bean.Groupon_rulesExample;
import com.cskaoyan.mapper.Groupon_rulesMapper;
import com.cskaoyan.vo.GroupBuyingsVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class GroupBuyingsVoServiceImpl implements GroupBuyingsVoService {


    @Autowired
    Groupon_rulesMapper groupon_rulesMapper;

    @Override
    public GroupBuyingsVo query(Integer page, Integer limit, String sort, String order) {

        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset
        Groupon_rulesExample groupon_rulesExample = new Groupon_rulesExample();
        groupon_rulesExample.setOrderByClause(sort + " " + order);//${}做Sql拼接
        List<Groupon_rules> groupons = groupon_rulesMapper.selectByExample(groupon_rulesExample);
        PageInfo<Groupon_rules> groupon_rulesPageInfo = new PageInfo<>(groupons);
//        PageInfo<User> userPageInfo = new PageInfo<>(users);//查询结果放进来
        long total = groupon_rulesPageInfo.getTotal();
//        long total = userPageInfo.getTotal();//总的数据量 👉 select count(*) 条件是byExample
        GroupBuyingsVo groupBuyingsVo = new GroupBuyingsVo();
//        UserVo userVo = new UserVo();
        groupBuyingsVo.setItems(groupons);
//        userVo.setItems(users);
        groupBuyingsVo.setTotal(total);
//        userVo.setTotal(total);
//        return userVo;
        return groupBuyingsVo;
    }

    @Override
    public GroupBuyingsVo found(Integer page, Integer limit, String sort, String order, Integer goodsId) {
        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset
        Groupon_rulesExample example = new Groupon_rulesExample();
        Groupon_rulesExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        example.setOrderByClause(sort + " " + order  );//${}做Sql拼接
        List<Groupon_rules> groupons = groupon_rulesMapper.selectByExample(example);
        PageInfo<Groupon_rules> groupon_rulesPageInfo = new PageInfo<>(groupons);
        long total = groupon_rulesPageInfo.getTotal();
        GroupBuyingsVo groupBuyingsVo = new GroupBuyingsVo();
        groupBuyingsVo.setItems(groupons);
        groupBuyingsVo.setTotal(total);
        return groupBuyingsVo;

    }
}
